# Linux makefile for SheepShaver

## System specific configuration

SHELL = /bin/sh

prefix = /usr/local
exec_prefix = ${prefix}
bindir = ${exec_prefix}/bin
datadir = ${prefix}/share
mandir = ${prefix}/man
man1dir = $(mandir)/man1

DESTDIR =

CC = gcc
CXX = g++
CFLAGS = -g -O2 -mdynamic-no-pic
CXXFLAGS = -g -O2 -mdynamic-no-pic
CPPFLAGS =  -I../Frameworks/SDL.framework/Headers -I../kpx_cpu/include -I../kpx_cpu/src -DUSE_JIT -I../include -I. -I../slirp
DEFS = -DHAVE_CONFIG_H -D_REENTRANT -DDATADIR=\"$(datadir)/$(APP)\"
LDFLAGS =  -pagezero_size 0x3000 -Wl,-seg1addr,0x78048000
LIBS = -lpthread -lm -framework SDL -framework Cocoa -framework Carbon -framework IOKit -framework CoreFoundation -framework CoreServices
SYSSRCS = ../SDL/video_sdl.cpp ../MacOSX/extfs_macosx.mm serial_unix.cpp ether_unix.cpp ../dummy/scsi_dummy.cpp ../SDL/audio_sdl.cpp ../dummy/prefs_editor_dummy.cpp  ../MacOSX/sys_darwin.cpp ../MacOSX/clip_macosx.cpp
CPUSRCS =         ../kpx_cpu/src/cpu/jit/jit-cache.cpp         ../kpx_cpu/src/cpu/jit/basic-dyngen.cpp         ../kpx_cpu/src/cpu/ppc/ppc-dyngen.cpp         ../kpx_cpu/src/cpu/ppc/ppc-jit.cpp     ../kpx_cpu/src/mathlib/ieeefp.cpp     ../kpx_cpu/src/mathlib/mathlib.cpp     ../kpx_cpu/src/cpu/ppc/ppc-cpu.cpp     ../kpx_cpu/src/cpu/ppc/ppc-decode.cpp     ../kpx_cpu/src/cpu/ppc/ppc-execute.cpp     ../kpx_cpu/src/cpu/ppc/ppc-translate.cpp     ../kpx_cpu/src/utils/utils-cpuinfo.cpp ../kpx_cpu/sheepshaver_glue.cpp
MONSRCS = 
PERL = /usr/bin/perl
USE_DYNGEN = yes
DYNGENSRCS =         ../kpx_cpu/src/cpu/jit/dyngen.c         ../kpx_cpu/src/cpu/jit/cxxdemangle.cpp
DYNGEN_CC = g++
DYNGEN_CFLAGS = $(CFLAGS)
DYNGEN_CXXFLAGS = $(CXXFLAGS)
DYNGEN_OP_FLAGS = -fomit-frame-pointer -fno-align-functions -finline-functions -finline-limit=10000 -fno-exceptions -g0 -fno-reorder-blocks -fno-optimize-sibling-calls
BLESS = Darwin/lowmem
EXEEXT = 
KEYCODES = ../SDL/keycodes
INSTALL = /usr/bin/install -c
INSTALL_PROGRAM = ${INSTALL} -s
INSTALL_DATA = ${INSTALL} -m 644

SLIRP_CFLAGS = -fno-strict-aliasing
SLIRP_SRCS =     ../slirp/bootp.c     ../slirp/ip_output.c  ../slirp/tcp_input.c      ../slirp/cksum.c     ../slirp/mbuf.c       ../slirp/tcp_output.c     ../slirp/debug.c     ../slirp/misc.c       ../slirp/tcp_subr.c       ../slirp/if.c        ../slirp/sbuf.c       ../slirp/tcp_timer.c      ../slirp/ip_icmp.c   ../slirp/slirp.c      ../slirp/tftp.c           ../slirp/ip_input.c  ../slirp/socket.c     ../slirp/udp.c
SLIRP_OBJS = $(SLIRP_SRCS:../slirp/%.c=obj/%.o)

STANDALONE_GUI = no
GUI_CFLAGS = 
GUI_LIBS = 
GUI_SRCS = ../prefs.cpp prefs_unix.cpp prefs_editor_gtk.cpp ../prefs_items.cpp \
	../user_strings.cpp user_strings_unix.cpp xpram_unix.cpp sys_unix.cpp rpc_unix.cpp

# Append disassembler to dyngen, if available
ifneq (:no,$(MONSRCS):$(USE_DYNGEN))
DYNGENSRCS += $(filter %i386-dis.c,$(MONSRCS))
endif

## Files
SRCS = SDLMain.m ../main.cpp main_unix.cpp ../prefs.cpp ../prefs_items.cpp prefs_unix.cpp sys_unix.cpp \
    ../rom_patches.cpp ../rsrc_patches.cpp ../emul_op.cpp ../name_registry.cpp \
    ../macos_util.cpp ../timer.cpp timer_unix.cpp ../xpram.cpp xpram_unix.cpp \
    ../adb.cpp ../sony.cpp ../disk.cpp ../cdrom.cpp ../scsi.cpp \
    ../gfxaccel.cpp ../video.cpp video_blit.cpp ../audio.cpp ../ether.cpp ../thunks.cpp \
    ../serial.cpp ../extfs.cpp \
    about_window_unix.cpp ../user_strings.cpp user_strings_unix.cpp \
    vm_alloc.cpp sigsegv.cpp rpc_unix.cpp \
    sshpty.c strlcpy.c $(SYSSRCS) $(CPUSRCS) $(MONSRCS) $(SLIRP_SRCS)
APP = SheepShaver
APP_EXE = $(APP)$(EXEEXT)
APP_APP = $(APP).app

PROGS = $(APP_EXE)
ifeq ($(STANDALONE_GUI),yes)
GUI_APP = SheepShaverGUI
GUI_APP_EXE = $(GUI_APP)$(EXEEXT)
GUI_APP_APP = $(GUI_APP).app
PROGS += $(GUI_APP_EXE)
else
CXXFLAGS += $(GUI_CFLAGS)
LIBS += $(GUI_LIBS)
endif

## Rules
.PHONY: modules install uninstall clean distclean depend dep
.SUFFIXES:
.SUFFIXES: .c .cpp .S .o .h

all: $(PROGS)

OBJ_DIR = obj
$(OBJ_DIR)::
	@[ -d $(OBJ_DIR) ] || mkdir $(OBJ_DIR) > /dev/null 2>&1

define SRCS_LIST_TO_OBJS
	$(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(foreach file, $(SRCS), \
	$(basename $(notdir $(file))))))
endef
OBJS = $(SRCS_LIST_TO_OBJS)

define GUI_SRCS_LIST_TO_OBJS
	$(addprefix $(OBJ_DIR)/, $(addsuffix .go, $(foreach file, $(GUI_SRCS), \
	$(basename $(notdir $(file))))))
endef
GUI_OBJS = $(GUI_SRCS_LIST_TO_OBJS)

define DYNGENSRCS_LIST_TO_OBJS
	$(addprefix $(OBJ_DIR)/, $(addsuffix .dgo, $(foreach file, $(DYNGENSRCS), \
	$(basename $(notdir $(file))))))
endef
DYNGENOBJS = $(DYNGENSRCS_LIST_TO_OBJS)

SRC_PATHS += $(sort $(foreach file, $(SRCS), $(dir $(file))))
VPATH :=
VPATH += $(addprefix :, $(subst  ,:, $(filter-out $($(subst, :, ,$(VPATH))), $(SRC_PATHS))))

$(APP_EXE): $(OBJ_DIR) $(OBJS)
	$(CXX) -o $@ $(LDFLAGS) $(OBJS) $(LIBS)
	$(BLESS) $(APP_EXE)

$(GUI_APP_EXE): $(OBJ_DIR) $(GUI_OBJS)
	$(CXX) -o $@ $(LDFLAGS) $(GUI_OBJS) $(GUI_LIBS)

$(APP)_app: $(APP) ../MacOSX/Info.plist ../MacOSX/$(APP).icns
	mkdir -p $(APP_APP)/Contents
	cp -f ../MacOSX/Info.plist $(APP_APP)/Contents/
	echo -n 'APPL????' > $(APP_APP)/Contents/PkgInfo
	mkdir -p $(APP_APP)/Contents/MacOS
	cp -f $(APP) $(APP_APP)/Contents/MacOS/
	strip $(APP_APP)/Contents/MacOS/$(APP)
	mkdir -p $(APP_APP)/Contents/Resources
	cp -f ../MacOSX/SheepShaver.icns $(APP_APP)/Contents/Resources/
	cp -prf ../Frameworks $(APP_APP)/Contents/

$(GUI_APP)_app: $(GUI_APP) ../MacOSX/Info.plist ../MacOSX/$(APP).icns
	mkdir -p $(GUI_APP_APP)/Contents
	sed -e "s/$(APP)/$(GUI_APP)/" < ../MacOSX/Info.plist > $(GUI_APP_APP)/Contents/Info.plist
	echo -n 'APPL????' > $(GUI_APP_APP)/Contents/PkgInfo
	mkdir -p $(GUI_APP_APP)/Contents/MacOS
	cp -f $(GUI_APP) $(GUI_APP_APP)/Contents/MacOS/
	strip $(GUI_APP_APP)/Contents/MacOS/$(GUI_APP)
	mkdir -p $(GUI_APP_APP)/Contents/Resources
	cp -f ../MacOSX/$(APP).icns $(GUI_APP_APP)/Contents/Resources/$(GUI_APP).icns

modules:
	cd Linux/NetDriver; make

install: $(PROGS) installdirs
	$(INSTALL_PROGRAM) $(APP_EXE) $(DESTDIR)$(bindir)/$(APP_EXE)
	if test -f "$(GUI_APP_EXE)"; then \
	  $(INSTALL_PROGRAM) $(GUI_APP_EXE) $(DESTDIR)$(bindir)/$(GUI_APP_EXE); \
	fi
	-$(INSTALL_DATA) $(APP).1 $(DESTDIR)$(man1dir)/$(APP).1
	$(INSTALL_DATA) $(KEYCODES) $(DESTDIR)$(datadir)/$(APP)/keycodes
	$(INSTALL_DATA) tunconfig $(DESTDIR)$(datadir)/$(APP)/tunconfig
	chmod 755 $(DESTDIR)$(datadir)/$(APP)/tunconfig

installdirs:
	$(SHELL) mkinstalldirs $(DESTDIR)$(bindir) $(DESTDIR)$(man1dir) $(DESTDIR)$(datadir)/$(APP)

uninstall:
	rm -f $(DESTDIR)$(bindir)/$(APP_EXE)
	rm -f $(DESTDIR)$(bindir)/$(GUI_APP_EXE)
	rm -f $(DESTDIR)$(man1dir)/$(APP).1
	rm -f $(DESTDIR)$(datadir)/$(APP)/keycodes
	rm -f $(DESTDIR)$(datadir)/$(APP)/tunconfig
	rmdir $(DESTDIR)$(datadir)/$(APP)

clean:
	rm -f $(PROGS) $(OBJ_DIR)/* core* *.core *~ *.bak ppc-execute-impl.cpp
	rm -f dyngen basic-dyngen-ops.hpp ppc-dyngen-ops.hpp ppc_asm.out.s
	rm -rf $(APP_APP) $(GUI_APP_APP)

distclean: clean
	rm -rf $(OBJ_DIR)
	rm -f Makefile
	rm -f config.cache config.log config.status config.h
	rm -f ../MacOSX/Info.plist

depend dep:
	makedepend $(CPPFLAGS) -Y. $(SRCS) 2>/dev/null

$(OBJ_DIR)/%.o : ../slirp/%.c
	$(CC) $(CPPFLAGS) $(DEFS) $(CFLAGS) $(SLIRP_CFLAGS) -c $< -o $@
$(OBJ_DIR)/%.o : %.c
	$(CC) $(CPPFLAGS) $(DEFS) $(CFLAGS) -c $< -o $@
$(OBJ_DIR)/%.o : %.cpp
	$(CXX) $(CPPFLAGS) $(DEFS) $(CXXFLAGS) -c $< -o $@
$(OBJ_DIR)/%.o : %.m
	$(CXX) $(CPPFLAGS) $(DEFS) $(CXXFLAGS) -c $< -o $@
$(OBJ_DIR)/%.o : %.mm
	$(CXX) $(CPPFLAGS) $(DEFS) $(CXXFLAGS) -c $< -o $@
$(OBJ_DIR)/%.o : %.S
	$(CPP) $(CPPFLAGS) -D__ASSEMBLY__ $< -o $*.out.s
	$(AS) $(ASFLAGS) -o $@ $*.out.s
	rm $*.out.s
$(OBJ_DIR)/%.go : %.cpp
	$(CXX) $(CPPFLAGS) $(DEFS) $(CXXFLAGS) $(GUI_CFLAGS) -DSTANDALONE_GUI -c $< -o $@

# Kheperix CPU emulator
kpxsrcdir = ../kpx_cpu/src
GENEXECPL = $(kpxsrcdir)/cpu/ppc/genexec.pl
DYNGEN    = dyngen$(EXEEXT)

ifeq ($(USE_DYNGEN),yes)
DYNGENDEPS = basic-dyngen-ops.hpp ppc-dyngen-ops.hpp

# Only GCC is supported for generating synthetic opcodes
$(DYNGEN): $(DYNGENOBJS)
	$(DYNGEN_CC) -o $@ $(LDFLAGS) $(DYNGENOBJS)

$(OBJ_DIR)/%.dgo : %.c
	$(DYNGEN_CC) -xc $(CPPFLAGS) $(DEFS) $(DYNGEN_CFLAGS) -c $< -o $@
$(OBJ_DIR)/%.dgo : %.cpp
	$(DYNGEN_CC) $(CPPFLAGS) $(DEFS) $(DYNGEN_CXXFLAGS) -c $< -o $@

$(OBJ_DIR)/basic-dyngen.o: basic-dyngen-ops.hpp
$(OBJ_DIR)/basic-dyngen-ops.o: $(kpxsrcdir)/cpu/jit/basic-dyngen-ops.cpp
	$(DYNGEN_CC) $(CPPFLAGS) $(DEFS) $(DYNGEN_CXXFLAGS) $(DYNGEN_OP_FLAGS) -c $< -o $@
basic-dyngen-ops.hpp: $(OBJ_DIR)/basic-dyngen-ops.o $(DYNGEN)
	./$(DYNGEN) -o $@ $<

$(OBJ_DIR)/ppc-dyngen.o: ppc-dyngen-ops.hpp
$(OBJ_DIR)/ppc-dyngen-ops.o: $(kpxsrcdir)/cpu/ppc/ppc-dyngen-ops.cpp basic-dyngen-ops.hpp
	$(DYNGEN_CC) $(CPPFLAGS) $(DEFS) $(DYNGEN_CXXFLAGS) $(DYNGEN_OP_FLAGS) -c $< -o $@
ppc-dyngen-ops.hpp: $(OBJ_DIR)/ppc-dyngen-ops.o $(DYNGEN)
	./$(DYNGEN) -o $@ $<

$(OBJ_DIR)/sheepshaver_glue.o $(OBJ_DIR)/ppc-cpu.o $(OBJ_DIR)/ppc-decode.o $(OBJ_DIR)/ppc-translate.o $(OBJ_DIR)/ppc-jit.o: basic-dyngen-ops.hpp ppc-dyngen-ops.hpp
endif

$(OBJ_DIR)/ppc-execute.o: ppc-execute-impl.cpp
ppc-execute-impl.cpp: $(kpxsrcdir)/cpu/ppc/ppc-decode.cpp $(GENEXECPL) $(DYNGENDEPS)
	$(CPP) $(CPPFLAGS) -DGENEXEC $< | $(PERL) $(GENEXECPL) > $@

# PowerPC CPU tester
TESTSRCS_ = mathlib/ieeefp.cpp mathlib/mathlib.cpp cpu/ppc/ppc-cpu.cpp cpu/ppc/ppc-decode.cpp cpu/ppc/ppc-execute.cpp cpu/ppc/ppc-translate.cpp test/test-powerpc.cpp $(MONSRCS) vm_alloc.cpp utils/utils-cpuinfo.cpp
ifeq ($(USE_DYNGEN),yes)
TESTSRCS_ += cpu/jit/jit-cache.cpp cpu/jit/basic-dyngen.cpp cpu/ppc/ppc-dyngen.cpp cpu/ppc/ppc-jit.cpp
endif
TESTSRCS  = $(TESTSRCS_:%.cpp=$(kpxsrcdir)/%.cpp)

define TESTSRCS_LIST_TO_OBJS
	$(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(foreach file, $(TESTSRCS), \
	$(basename $(notdir $(file))))))
endef
TESTOBJS  = $(TESTSRCS_LIST_TO_OBJS)

$(OBJ_DIR)/test-powerpc.o: $(kpxsrcdir)/test/test-powerpc.cpp
	$(CXX) $(CPPFLAGS) $(DEFS) $(CXXFLAGS) -DEMU_KHEPERIX -c $< -o $@

test-powerpc$(EXEEXT): $(TESTOBJS)
	$(CXX) -o $@ $(LDFLAGS) $(TESTOBJS) $(LIBS)

#-------------------------------------------------------------------------
# DO NOT DELETE THIS LINE -- make depend depends on it.
